package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonService;
import com.oa.admin.dto.DocumentAddDTO;
import com.oa.admin.dto.DocumentListDTO;
import com.oa.admin.dto.DocumentUpdateDTO;
import com.oa.admin.entity.DocumentFile;
import com.oa.admin.entity.DocumentPermission;
import com.oa.admin.service.IDocumentFileService;
import com.oa.admin.service.IDocumentPermissionService;
import com.oa.admin.vo.DocumentVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.Document;
import com.oa.admin.mapper.DocumentMapper;
import com.oa.admin.service.IDocumentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文档表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class DocumentServiceImpl extends ServiceImpl<DocumentMapper, Document> implements IDocumentService {

    private final DocumentMapper documentMapper;

    @Autowired
    private IDocumentPermissionService documentPermissionService;

    @Autowired
    private IDocumentFileService documentFileService;

    @Autowired
    public DocumentServiceImpl(DocumentMapper documentMapper) {
        this.documentMapper = documentMapper;
    }


    @Override
    @CommonService
    public PageResult<DocumentVO> findDocumentList(DocumentListDTO documentListDTO) {
        Page<DocumentVO> page = documentListDTO.getPage();
        return new PageResult(documentMapper.findDocumentList(page, documentListDTO));
    }

    @Override
    @CommonService
    public DocumentVO findDocumentById(String id) {
        return documentMapper.findDocumentById(id);
    }

    @Override
    @CommonService
    public boolean documentAdd(DocumentAddDTO documentAddDTO) {
        Document document = new Document();
        BeanUtils.copyProperties(documentAddDTO, document);
        document.setCreateTime(new Date());
        if(insert(document)){
            if (documentAddDTO.getDocumentPermissionAddDTOList() != null && documentAddDTO.getDocumentPermissionAddDTOList().size() > 0){
                List<DocumentPermission> documentPermissions = new ArrayList<>();
                documentAddDTO.getDocumentPermissionAddDTOList().forEach(d -> {
                    DocumentPermission documentPermission = new DocumentPermission();
                    BeanUtils.copyProperties(d, documentPermission);
                    documentPermission.setDocumentId(document.getId());
                    documentPermissions.add(documentPermission);
                });
                documentPermissionService.insertBatch(documentPermissions);
            }
//            if (documentAddDTO.getDocumentFileAddDTOList() != null && documentAddDTO.getDocumentFileAddDTOList().size() > 0){
//                List<DocumentFile> documentFiles = new ArrayList<>();
//                documentAddDTO.getDocumentFileAddDTOList().forEach(d -> {
//                    DocumentFile documentFile = new DocumentFile();
//                    BeanUtils.copyProperties(d, documentFile);
//                    documentFile.setCreateTime(new Date());
//                    documentFile.setDocumentId(document.getId());
//                    documentFiles.add(documentFile);
//                });
//                documentFileService.insertBatch(documentFiles);
//            }
            return true;
        }
        return false;
    }

    @Override
    @CommonService
    public boolean documentUpdate(DocumentUpdateDTO documentUpdateDTO) {
        Document document = new Document();
        BeanUtils.copyProperties(documentUpdateDTO, document);
        if(updateById(document)){
            if (documentUpdateDTO.getDocumentPermissionAddDTOList() != null && documentUpdateDTO.getDocumentPermissionAddDTOList().size() > 0){
                if(documentPermissionService.delete(new EntityWrapper<DocumentPermission>().eq("document_id", documentUpdateDTO.getId()))){
                    List<DocumentPermission> documentPermissions = new ArrayList<>();
                    documentUpdateDTO.getDocumentPermissionAddDTOList().forEach(d -> {
                        DocumentPermission documentPermission = new DocumentPermission();
                        BeanUtils.copyProperties(d, documentPermission);
                        documentPermission.setDocumentId(document.getId());
                        documentPermissions.add(documentPermission);
                    });

                    documentPermissions.forEach(d -> d.setDocumentId(document.getId()));
                    documentPermissionService.insertBatch(documentPermissions);

                }else {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}
